package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetMessageListBean;
import com.rjwh.dingdong.client.bean.localbean.GetMessageListBean;
import com.rjwh.dingdong.client.bean.localbean.MessageListItemData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MessageListModel;
import com.rjwh.dingdong.client.mvp.presenter.IMessageListPresenter;
import com.rjwh.dingdong.client.mvp.view.MessageListView;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.util.List;

/**
 * @author Kay
 * @ClassName MessageListPresenter
 * @Description
 * @date 2016-12-27
 * @history 2016-12-27 author: description:
 */

public class MessageListPresenter extends BasePresenter<MessageListView> implements IMessageListPresenter {

    private MessageListModel model;
    private SwipyRefreshLayoutDirection mDirection;

    public MessageListPresenter(Context context, MessageListView mMvpView) {
        super(context, mMvpView);
        model = new MessageListModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            switch (businessType) {
                case NetConstant.GETMESSAGELIST:
                    getMvpView().setRefreshing(false);
                    ResGetMessageListBean resGetMessageListBean = (ResGetMessageListBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetMessageListBean != null) {
                        GetMessageListBean data = resGetMessageListBean.getData();
                        if (data != null) {
                            List<MessageListItemData> messagelist = data.getMessagelist();
                            if (messagelist != null) {
                                if (messagelist.isEmpty()) {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                                    if (mDirection == SwipyRefreshLayoutDirection.BOTTOM) {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data_more));
                                    } else {
                                        ToastUtil.showToast(getContext(), getContext().getString(R.string.empty_data));
                                    }
                                } else {
                                    getMvpView().setDirection(SwipyRefreshLayoutDirection.BOTH);
                                }
                                getMvpView().loadData(isRefresh, messagelist);
                            }
                        }
                    } else {
                        getMvpView().setDirection(SwipyRefreshLayoutDirection.TOP);
                        ToastUtil.showToast(getContext(), getContext().getString(R.string.refresh_error));
                    }

                    break;

                default:
                    break;
            }
        }
    }

    /**
     * 获取消息列表
     *
     * @param category
     * @param pageindex
     * @param isRefresh
     * @param direction
     */
    @Override
    public void doGetMessageList(String category, int pageindex, boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        mDirection = direction;
        model.doGetMessageList(category, pageindex, isRefresh);
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
